package demo6;

import java.util.Arrays;

public class Test2 {
    //堆排序
    public void heapSort(int[] arr) {
        //先创建一个大根堆;
        createBigHeap(arr);
        int end = arr.length - 1;
        while(end > 0) {
            swap(arr, 0, end);
            adjustDown(arr, 0, end);
            end--;
        }
    }

    private void createBigHeap(int[] arr) {
        int len = arr.length;
        for(int parent = (len - 1 - 1) / 2; parent >= 0; parent--) {
            //对每颗子树向下调整
            adjustDown(arr, parent, len);
        }
    }

    private void adjustDown(int[] arr, int parent, int len) {
        int child = parent * 2 + 1;
        while(child < len) {
            if(child + 1 < len && arr[child + 1] > arr[child]) {
                child++;
            }
            if(arr[parent] < arr[child]) {
                //交换
                swap(arr, parent, child);
                //继续向下调整
                parent = child;
                child = child * 2 + 1;
            } else {
                break;
            }
        }
    }

    private void swap(int[] arr, int parent, int child) {
        int tmp = arr[parent];
        arr[parent] = arr[child];
        arr[child] = tmp;
    }


    //快速排序
    public void quickSort(int[] arr) {
        quick(arr, 0, arr.length - 1);
    }

    private void quick(int[] arr, int left, int right) {
        if(left >= right) {
            return;
        }
        //递归层次优化
        if (right - left + 1 <= 2) {
            //最后七层使用insert排序
            quickInsertSort(arr, left, right);
            return;
        }
        //三数取中优化
        int mid = fundMidIndex(arr, left, right);
        swap(arr, left, mid);
        //寻找基准
        int point = fundPoint(arr, left, right);
        quick(arr, left, point - 1);
        quick(arr, point + 1, right);
    }

    private void quickInsertSort(int[] arr, int left, int right) {
        for(int i = left + 1; i <= right; i++) {
            int key = arr[i];
            int j = i - 1;
            for(; j >= left; j--) {
                if(arr[j] > key) {
                    arr[j + 1] = arr[j];
                } else {
                    break;
                }
            }
            arr[j + 1] = key;
        }
    }

    private int fundMidIndex(int[] arr, int left, int right) {
        int mid = (left + right) / 2;
        if(arr[left] < arr[right]) {
            if(arr[mid] < arr[left]) {
                return left;
            } else if(arr[mid] > arr[right]) {
                return right;
            } else {
                return mid;
            }
        } else {
            if(arr[mid] > arr[left]) {
                return left;
            } else if(arr[mid] < arr[right]) {
                return right;
            } else {
                return mid;
            }
        }
    }

    private int fundPoint(int[] arr, int start, int end) {
        int key = arr[start];
        while(start < end) {
            while(start < end && key <= arr[end]) {
                end--;
            }
            arr[start] = arr[end];
            while(start < end && arr[start] <= key) {
                start++;
            }
            arr[end] = arr[start];
        }
        arr[start] = key;
        return start;
    }
}

class Main2 {
    public static void main(String[] args) {
        Test2 t = new Test2();
        int[] arr = {4, 1, 6, 2, 7, 8, 3, 0, 9, 5};
        t.quickSort(arr);
        System.out.println(Arrays.toString(arr));
    }
}
